package com.android.pantryshopping;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.pantryshopping.bean.Commodity;

import java.util.List;

public class DetailsActivity extends AppCompatActivity {

    private static final String TAG = "DetailsActivity";

    private CommoditySqLiteHelper mCommoditySqLiteHelper;

    private Commodity mCommodity;

    private TextView mTvId;
    private TextView mTvName;
    private TextView mTvPrice;
    private TextView mTvNum;
    private Button mBtnDelete;
    private Button mBtnEdit;
    private Button mBtnBack;
    private Button mBtnDetermine;
    private boolean mIsShopList;
    private ImageView mImgShop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        findViewById(R.id.btn_back).setOnClickListener(v -> {
            finish();
        });
        mBtnDelete.setOnClickListener(v -> {
            mCommoditySqLiteHelper.deleteCommodityData(mCommodity);
            Toast.makeText(this,"Delete Success",Toast.LENGTH_SHORT).show();
            finish();
        });
        mBtnEdit.setOnClickListener(v -> {
            mTvId.setEnabled(true);
            mTvName.setEnabled(true);
            mTvPrice.setEnabled(true);
            mTvNum.setEnabled(true);
        });
        findViewById(R.id.btn_determine).setOnClickListener(v -> {
            Commodity commodity = new Commodity();
            commodity.setId(mTvId.getText().toString().trim());
            commodity.setName(mTvName.getText().toString().trim());
            commodity.setImage(mCommodity.getImage());
            commodity.setPrice(mTvPrice.getText().toString().trim());
            commodity.setQuantity(mTvNum.getText().toString().trim());
            mCommoditySqLiteHelper.updateCommodityData(commodity);
            finish();
        });
    }

    private void initData() {
        int position = getIntent().getIntExtra("position", 0);
        mIsShopList = getIntent().getBooleanExtra("isShopList", true);

        if (!mIsShopList) {
            mBtnEdit.setVisibility(View.GONE);
            mBtnDelete.setVisibility(View.GONE);
            mBtnBack.setVisibility(View.GONE);
            mBtnDetermine.setVisibility(View.GONE);
        }

        mCommoditySqLiteHelper = new CommoditySqLiteHelper(this,"Commodity_db",null,1);
        List<Commodity> commodities = mCommoditySqLiteHelper.queryAllCommodityData();
        mCommodity = commodities.get(position);

        mTvId.setText(mCommodity.getId());
        Bitmap bitmap = BitmapFactory.decodeFile(mCommodity.getImage());
        mImgShop.setImageBitmap(bitmap);
        mTvName.setText(mCommodity.getName());
        mTvPrice.setText(mCommodity.getPrice());
        mTvNum.setText(mCommodity.getQuantity());
        Log.d(TAG,"position-->" + position);
    }

    private void initView() {
        mTvId = findViewById(R.id.tv_id);
        mImgShop = findViewById(R.id.img_shop);
        mTvName = findViewById(R.id.tv_name);
        mTvPrice = findViewById(R.id.tv_price);
        mTvNum = findViewById(R.id.tv_num);
        mBtnEdit = findViewById(R.id.btn_edit);
        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnBack = findViewById(R.id.btn_back);
        mBtnDetermine = findViewById(R.id.btn_determine);
    }
}